package cn.edu.abtu.utils;

import cn.edu.abtu.entity.SysDepartment;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class DepartmentTree {

    public static List<SysDepartment> makeDepartmentTree(List<SysDepartment> departmentList, Long parentId){
        List<SysDepartment> list = new ArrayList<SysDepartment>();
        Optional.ofNullable(departmentList).orElse(new ArrayList<SysDepartment>())
                .stream().filter(item -> item != null && item.getParentId().equals(parentId))
                .forEach(item ->{
                    SysDepartment sysDepartment = new SysDepartment();
                    BeanUtils.copyProperties(item,sysDepartment);
                    List<SysDepartment> children = makeDepartmentTree(departmentList, item.getId());
                    sysDepartment.setChildren(children);
                    list.add(sysDepartment);
                });
        return list;
    }
}
